package com.ruoyi.bearing.controller;

import com.ruoyi.bearing.domain.BearingModel;
import com.ruoyi.bearing.service.IBearingFeatureService;
import com.ruoyi.bearing.service.IBearingSignalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.exception.flask.FlaskException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 特征分析
 */
@Api("特征分析")
@RestController
@RequestMapping("/bearingData/feature")
public class BearingFeatureController extends BaseController {
    @Autowired
    private IBearingFeatureService iBearingFeatureService;

    /**
     * 特征提取 使用HttpServletResponse的controller在get请求中可能接收不到前端数据
     */
    @ApiOperation("特征提取")
    @PreAuthorize("@ss.hasPermi('bearingData:feature:featureExtraction')")
    @PostMapping("/featureExtraction")
    @Transactional(rollbackFor = Exception.class)
    public void featureExtraction(@RequestParam("files") MultipartFile[] files, BearingModel bearingModel, HttpServletResponse response) throws IOException, IllegalAccessException {
        String res = iBearingFeatureService.startBearingFeatureExtraction(files[0], bearingModel);
        if (null == res) {
            throw new FlaskException();
        }
        iBearingFeatureService.csvToVue(res, response.getOutputStream());
    }

    /**
     * 特征选择
     */
    @ApiOperation("特征选择")
    @PreAuthorize("@ss.hasPermi('bearingData:feature:featureSelection')")
    @PostMapping("/featureSelection")
    public void featureSelection(@RequestParam("files") MultipartFile[] files, BearingModel bearingModel, HttpServletResponse response) throws IOException, IllegalAccessException {
        String res = iBearingFeatureService.startBearingFeatureSelection(files[0], bearingModel);
        if (null == res) {
            throw new FlaskException();
        }
        iBearingFeatureService.csvToVue(res, response.getOutputStream());
    }

}
